/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.user;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import model.user.Director;
import model.user.Governor;
import model.user.Staff;
import model.user.User;
import util.Utilization;
import view.user.MainFrame;
import view.user.LoginFrame;

/**
 *
 * @author CUNEYT
 */
public class LoginController {

    private class LoginAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            boolean success = false;
            if ((lf.getUsernameText().isEmpty()) || (lf.getPassword().length < 1)) {
                JOptionPane.showMessageDialog(lf, "Username/Password cannot be empty!", "Invalid Login", JOptionPane.ERROR_MESSAGE);
            } else {
                Iterator<User> it = users.iterator();
                User user = null;
                while (it.hasNext() && !success) {
                    user = it.next();
                    if ((user.getUsername().equals(lf.getUsernameText())) && (Arrays.equals(user.getPassword(), lf.getPassword()))) {
                        success = true;
                    }
                }
                if (!success) {
                    JOptionPane.showMessageDialog(lf, "Wrong Username/Password", "Invalid Login", JOptionPane.ERROR_MESSAGE);
                } else {
                    if (user.getRole().equals(User.DIRECTOR)) {
                        Director director = new Director(user.getName(), user.getSurname(), user.getUsername(), user.getPassword());
                        Utilization.prepareDirector(director);
                        MainFrame mf = new MainFrame(user);
                        DirectorController dc = new DirectorController(director, mf.getDirectorView(), mf);
                        dc.changeForePanel(dc.getdView().getListPanel());
                        mf.changeForePanel(dc.getdView());
                        mf.setVisible(true);
                        lf.dispose();
                    } else if (user.getRole().equals(User.STAFF)) {
                        Staff staff = new Staff(user.getName(), user.getSurname(), user.getUsername(), user.getPassword());

                        Utilization.prepareStaff(staff);
                        MainFrame mf = new MainFrame(user);
                        StaffController sc = new StaffController(staff, mf.getStaffView());
                        sc.changeForePanel(sc.getView().getSchedulePanel());
                        mf.changeForePanel(sc.getView());
                        mf.setVisible(true);
                        lf.dispose();
                    } else if (user.getRole().equals(User.GOVERNOR)) {
                        Governor governor = new Governor(user.getName(), user.getSurname(), user.getUsername(), user.getPassword());

                        Utilization.prepareGovernor(governor);
                        MainFrame mf = new MainFrame(user);
                        GovernorController gc = new GovernorController(governor, mf.getGovernorView());
                        gc.changeForePanel(mf.getGovernorView().getActiveCampaignsPanel());
                        mf.changeForePanel(mf.getGovernorView());
                        mf.setVisible(true);
                        lf.dispose();
                    } else if (user.getRole().equals(User.ADMIN)) {
                        MainFrame mf = new MainFrame(user);
                        AdminController ac = new AdminController(user, mf.getAdminView());
                        ac.changeForePanel(ac.getView().getUserPanel());
                        mf.changeForePanel(mf.getAdminView());
                        mf.setVisible(true);
                        lf.dispose();
                    } else {
                        try {
                            throw new Exception("Undefined user role!");
                        } catch (Exception ex) {
                            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        }
    }

    private class LoginCancelledAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
    private LoginFrame lf;
    private ArrayList<User> users;

    public LoginController(LoginFrame frame) {
        lf = frame;
        users = new ArrayList<User>();
        lf.getCancelButton().addActionListener(new LoginCancelledAction());
        lf.getLoginButton().addActionListener(new LoginAction());
        users = Utilization.getUsers();
        lf.setVisible(true);
        lf.setLocationRelativeTo(null);
    }
}
